package com.ug.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ug.common.constant.SecurityConstants;
import com.ug.common.response.ResponseMessage;
import com.ug.common.response.ResponseMessageFactory;
import com.ug.common.utils.StringUtil;
import com.ug.logs.annotation.Log;
import com.ug.system.entity.SysDict;
import com.ug.system.entity.dto.QueryDto;
import com.ug.system.entity.dto.SysDictDto;
import com.ug.system.service.SysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  字典前端控制器
 * </p>
 *
 * @author LY
 * @since 2024-08-13 10:56:19
 */
@Tag(name = "SysDictController", description = "字典管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/dict")
public class SysDictController {

    private final SysDictService sysDictService;

    /**
     * 查询字典列表
     * @param queryDto 请求查询信息入参
     * @param sysDictDto 请求查询字典dto
     */
    @Operation(summary = "字典列表", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @GetMapping("/list")
    public ResponseMessage<IPage<SysDict>> getLogList(QueryDto queryDto, SysDictDto sysDictDto){
        return ResponseMessageFactory.getSuccessResponseMessage(sysDictService.getSysDictList(queryDto, sysDictDto));
    }

    /**
     * 编辑字典信息
     * @param sysDictDto 请求信息入参
     */
    @Operation(summary = "编辑字典", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @PostMapping("/edit")
    public ResponseMessage<Void> editSysDict(@RequestBody SysDictDto sysDictDto){
        String str = StringUtil.getEditType(sysDictDto.getDictId());
        sysDictService.editDict(sysDictDto);
        return ResponseMessageFactory.getSuccessResponseMessage(str);
    }

    /**
     * 删除用户
     * @param dictId 请求字典id入参
     */
    @Operation(summary = "删除字典", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("删除字典")
    @DeleteMapping("/del")
    public ResponseMessage<String> delDict(@Parameter(description = "字典id") @RequestParam("dictId") Long dictId){
        sysDictService.deleteDict(dictId);
        return ResponseMessageFactory.getSuccessResponseMessage("删除成功");
    }

    /**
     * 修改字典状态
     * @param dictId 字典编号
     * @param enabled true:启用 false:停用
     */
    @Operation(summary = "改变字典状态", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("改变字典状态")
    @PutMapping("/enabled")
    public ResponseMessage<String> enableDict(@RequestParam("dictId") Long dictId, @RequestParam("enabled") Integer enabled){
        String str = enabled == 1 ? "启用" : "停用";
        sysDictService.enableDict(dictId, enabled);
        return ResponseMessageFactory.getSuccessResponseMessage(str + "成功");
    }
}
